package pacman.protocol;

import java.util.ArrayList;

import maze.Coordinate;
import maze.Location;
import maze.Maze;
import pacman.Game;
import robots.Ghost;

public class GhostProtocolProcessor {
	private Ghost ghost;
	
	public GhostProtocolProcessor(Ghost ghost){
		this.ghost = ghost;
	}
	
	/**
	 * Processes the DISCOVER command
	 */
	@SuppressWarnings("unused")
	public void processDISCOVER(Coordinate coord, int north, int east,	int south, int west, boolean ownCoord,String senderName) {
		if(!senderName.equals(ghost.getName()) && ghost.readySetGo){ //Je hebt geen maze voor jezelf en je moet wachten met dingen processen tot je je mazes geinitialiseerd hebt
			Ghost senderGhost = ghost.getGame().getGhostWithName(senderName);
			Maze senderMaze = ghost.getMazesOtherRobots().get(senderGhost.getName());
			if(senderGhost == null){
				System.err.println("SenderGhost was null in bij verwerking in " + ghost.getName());
			}
			if(senderMaze == null){
				System.err.println("senderMaze was null in bij verwerking in " + ghost.getName());
			}
			try{
				if(north != 0)
					senderMaze.write(coord.getX(), coord.getY(), maze.Direction.NORTH, (char) north, ownCoord);
				if(east != 0)
					senderMaze.write(coord.getX(), coord.getY(), maze.Direction.EAST, (char) east, ownCoord);
				if(south != 0)
					senderMaze.write(coord.getX(), coord.getY(), maze.Direction.SOUTH, (char) south, ownCoord);
				if(west != 0)
					senderMaze.write(coord.getX(), coord.getY(), maze.Direction.WEST, (char) west, ownCoord);
			} catch (NullPointerException e){
				System.err.println("NULLPOINTER IN PROCESSDISCOVER VAN " + ghost.getName() + " bij willen plaatsen in maze van " + senderName + "(" + senderGhost + ")");
				System.err.print("GhostMAZES van " + ghost.getName() + ": ");
				for(String mazeGhost: ghost.getMazesOtherRobots().keySet()){
					System.err.print(mazeGhost + "("+ mazeGhost + ")" + " - " + ghost.getMazesOtherRobots().get(mazeGhost) + " " );
				}
				System.err.println("");
			}
		}
	}

	/**
	 * Processes the CAPTURED command
	 *
	 */
	public void processCAPTURED() {
		getGhost().capturedPacman(true);
	}

	/**
	 * Processes the PLAN command.
	 */
	public void processPLAN(ArrayList<Location> plan) {
		getGhost().setPlan(plan);
	}
	
	public void processPING(String sender, String message){
		System.err.println("Gewone ghost wou processPING doen");
	}
	public void processPONG(String sender, String message){
		System.err.println("Gewone ghost wou processPONG doen");
	}
	
	public void processSHOWMAP(){
		System.err.println("Gewone ghost wou processSHOWMAP doen");
	}


	/**
	 * Processes the CANCELPLAN command.
	 */
	public void processCANCELPLAN() {
		getGhost().setPlan(null);
	}

	protected Ghost getGhost() {
		return ghost;
	}

	protected Game getGame(){
		return getGhost().getGame();
	}
	
}
